﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.WindowsMobile.Forms;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PDAMain
{
    class TakePicture
    {
        //string sysPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        public static string imagePath = @"\Storage Card\DCIMS";
        CameraCaptureDialog ccdialog = null;
        public TakePicture()
        {
            if (!Directory.Exists(imagePath))
            {
                Directory.CreateDirectory(imagePath);
            }

            ccdialog = new CameraCaptureDialog();
            //设置相机模式：still 拍照；VideoOnly 录像；VideoWithAudio 录像带声音
            ccdialog.Mode = CameraCaptureMode.Still;
            //存放默认的图片的目录
            ccdialog.InitialDirectory = imagePath;
            //设置图片分辨率(图片分辨率最大可以设置为2592*1728。但是如果设置成大分辨率，则无法将图片显示在PictureBox控件中，目前最大可以使用的分辨率为1584*1056)
            ccdialog.Resolution = new Size(240, 160);
            //设置拍照的质量
            //ccdialog.StillQuality = CameraCaptureStillQuality.High;
        }
        /// <summary>
        ///  自我毁灭，清除内存
        /// </summary>
        public void dispose()
        {
            if (ccdialog != null)
            {
                ccdialog.Dispose();
                ccdialog = null;
            }
        }

        /// <summary>
        /// 检查并创建图片存放的目录
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private bool checkFilePath(string filePath)
        {
            bool result = true;
            try
            {
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
            }
            catch(Exception ex)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="filename">图片名称</param>
        /// <returns>返回Bitmap类型可供PictureBox控件显示</returns>>
        public Bitmap Show(string folderName, string fileName)
        {
            Bitmap bitmap = null;
            string thisImagePath = imagePath;

            if (folderName != "") 
                thisImagePath = imagePath + "\\" + folderName;

            if (checkFilePath(thisImagePath))
            {
                //设置照片存放的目录
                ccdialog.InitialDirectory = thisImagePath;
                //设置照片名称
                ccdialog.DefaultFileName = fileName + ".jpg";
                DialogResult result = ccdialog.ShowDialog();
                 
                if (result == DialogResult.OK)
                {
                    //获取拍照图片的民初
                    System.IO.FileStream fileSt = new FileStream(ccdialog.FileName, FileMode.Open, FileAccess.Read);
                    System.IO.MemoryStream ms = null;

                    try
                    {
                        //将图片转换成byte数组
                        byte[] _ImageBytes = new Byte[fileSt.Length];
                        fileSt.Read(_ImageBytes, 0, _ImageBytes.Length);
                        fileSt.Close();
                        ms = new System.IO.MemoryStream(_ImageBytes);
                        bitmap = new Bitmap(ms);
                    }
                    catch (System.NotSupportedException ex)
                    {
                        fileSt.Close();
                    }
                    catch (System.OutOfMemoryException ex)
                    {
                        fileSt.Close();
                    }
                    catch (System.Exception ex)
                    {
                        fileSt.Close();
                    }
                }
            }
            return bitmap;
        }
        /*
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="filename">保存图片名称</param>
        /// <returns>true 拍照保存成功 false 拍照保存失败</returns>
        private bool Save(string fileName)
        {
            bool flag = false;
            try
            {
                if (checkFilePath(imagePath))
                {
                    ccdialog.DefaultFileName = fileName + ".jpg";
                    DialogResult result = ccdialog.ShowDialog();
                    flag = true;
                }
            }
            catch
            {

            }
            return flag;
        }
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="filename">保存图片名称</param>
        /// <param name="path">保存路径</param>
        /// <returns>true 拍照保存成功 false 拍照保存失败</returns>
        private bool Save_Path(string fileName, string path)
        {

            bool flag = true;
            try
            {
                if (checkFilePath(path))
                {
                    ccdialog.DefaultFileName = fileName + ".jpg";
                    DialogResult result = ccdialog.ShowDialog();
                }
                else
                {
                    flag = false;
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="filename">图片名称</param>
        /// <param name="path">保存路径</param>
        /// <returns>返回Bitmap类型可供PictureBox控件显示</returns>>
        private Bitmap Show_Path(string fileName, string path)
        {
            Bitmap bitmap = null;
            if (checkFilePath(path))
            {
                ccdialog.DefaultFileName = fileName + ".jpg";
                DialogResult result = ccdialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    //获取拍照图片的民初
                    System.IO.FileStream fileSt = new System.IO.FileStream(ccdialog.FileName, FileMode.Open, FileAccess.Read);
                    System.IO.MemoryStream ms = null;

                    try
                    {
                        //将图片转换成byte数组
                        byte[] _ImageBytes = new Byte[fileSt.Length];
                        fileSt.Read(_ImageBytes, 0, _ImageBytes.Length);
                        fileSt.Close();
                        ms = new System.IO.MemoryStream(_ImageBytes);
                        bitmap = new Bitmap(ms);
                    }
                    catch (System.NotSupportedException ex)
                    {
                        fileSt.Close();
                    }
                    catch (System.OutOfMemoryException ex)
                    {
                        fileSt.Close();
                    }
                    catch (System.Exception ex)
                    {
                        fileSt.Close();
                    }
                }
            }
            return bitmap;
        }
         * */
    }
}
